package com.project.forlearning.service.impl;

import com.alibaba.fastjson.JSON;
import com.project.forlearning.constant.ResponseTool;
import com.project.forlearning.dao.BackUserMapper;
import com.project.forlearning.pojo.User;
import com.project.forlearning.pojo.vo.ResponseInfo;
import com.project.forlearning.service.BackUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class BackUserServiceImpl implements BackUserService {

    @Autowired
    private BackUserMapper backUserMapper;

    /**
     * 查询用户
     * @param userId
     * @return
     */
    @Override
    public String findUser(Integer userId) {
        //id不为空
        if(userId <= 0)
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DATA_ERROR.code, ResponseTool.DATA_ERROR.msg, false));

        User user = backUserMapper.findUser(userId);

        if(user == null){
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_FAIL.code, ResponseTool.QUERY_FAIL.msg, false));
        }
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS.code, ResponseTool.QUERY_SUCCESS.msg, user));
    }

    /**
     * 查询所有用户
     * @return
     */
    @Override
    public String findAllUser() {
        List<User> user = backUserMapper.findAllUser();

        //若查询结果为空
        if(user == null){
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS_ALL.code, ResponseTool.QUERY_SUCCESS_ALL.msg, false));
        }
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS.code, ResponseTool.QUERY_SUCCESS.msg, user));
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @Override
    public String deleteUser(Integer userId) {
        //参数校验
        if(userId <= 0)
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DATA_ERROR.code, ResponseTool.DATA_ERROR.msg, false));

        //删除成功
        if(backUserMapper.deleteUser(userId)){
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_SUCCESS.code, ResponseTool.DELETE_SUCCESS.msg, true));
        }
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_FAIL.code, ResponseTool.DELETE_FAIL.msg, false));
    }




}
